package com.neuronbit.businessflow.flow.clazz;

import com.neuronbit.javaparser.ast.type.Type;
import com.neuronbit.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.neuronbit.javaparser.resolution.types.ResolvedReferenceType;

/**
 * The type Source class info.
 */
public class SourceClassInfo implements ClassInfo {
    private final ResolvedReferenceType type;

    /**
     * Instantiates a new Source class info.
     *
     * @param type the declaration
     */
    public SourceClassInfo(Type type) {
        if (!type.isReferenceType()) {
            throw new IllegalArgumentException("Type must be a reference type");
        }
        try {
            this.type = type.resolve().asReferenceType();
        } catch (Exception e) {
            throw new IllegalArgumentException("Type can not be resolved to a reference type, " + type.toDescriptor(), e);
        }
    }

    @Override
    public MethodInfo[] getMethods() {
        return type.getAllMethods().stream().map(methodDeclaration -> new SourceMethodInfo(this, methodDeclaration)).toArray(MethodInfo[]::new);
    }

    @Override
    public String getQualifiedName() {
        return type.getQualifiedName();
    }
}
